from rest_framework.generics import ListCreateAPIView
from rest_framework.permissions import IsAdminUser
from meiduo_admin.serializers.users import UserSerializer
from meiduo_admin.utils import PageNum
from users.models import User

class UserView(ListCreateAPIView):
    serializer_class = UserSerializer
    queryset = User.objects.all()
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    def get_queryset(self):
        # 1、获取前端的keyword参数
        keyword = self.request.GET.get('keyword')
        if keyword == '':
            return User.objects.filter(is_staff=False)
        else:
            # username__contains 模糊查询 包含
            return User.objects.filter(is_staff=False, username__contains=keyword)

